var CanvasUtils = Class.create();

CanvasUtils.prototype = {
	initialize : function(options) {
		CUR_CANVAS = this;
		this.canvas = document.getElementById((typeof (options) != "undefined" && typeof (options.id) != "undefined") ? options.id : "canvas");
		this.withPosition = (typeof (options) != "undefined" && typeof (options.withposition) != "undefined");
		this.color=(typeof (options) != "undefined" && typeof (options.id) != "undefined") ? options.color : "Green";
		this.ctx = canvas.getContext("2d");
		this.points = options ? options.points : null;
	},
	drawLine : function(points) {
		if (!points)
			points = CUR_CANVAS.points;
		var i;
		CUR_CANVAS.ctx.shadowOffsetX = 2;
		CUR_CANVAS.ctx.shadowOffsetY = 2;
		CUR_CANVAS.ctx.shadowBlur = 4;
		CUR_CANVAS.ctx.shadowColor = 'rgba(17, 19, 22, 0.5)';
		var gr = CUR_CANVAS.ctx.createLinearGradient(0, 0, 100, 0); // 设置线条变色
		gr.addColorStop(0, 'rgb(255,0,0)');
		gr.addColorStop(.5, 'rgb(0,255,0)');
		gr.addColorStop(1, 'rgb(255,0,0)');
		CUR_CANVAS.ctx.strokeStyle = CUR_CANVAS.color;
		CUR_CANVAS.ctx.fillStyle = CUR_CANVAS.color;
		CUR_CANVAS.ctx.font = "14px sans-serif";
		CUR_CANVAS.ctx.lineCap = "round";
		CUR_CANVAS.ctx.lineJoin = "round";
		for (i = 0; points[i + 1] != null; i++) {
			if (i == 0) {
				CUR_CANVAS.ctx.beginPath();
				CUR_CANVAS.ctx.arc(points[i].x - LEFT, points[i].y - TOP, 3, 0, 2 * Math.PI, false);
				if (CUR_CANVAS.withPosition)
					CUR_CANVAS.ctx.fillText("("+(points[i + 1].x - LEFT) + "," + (points[i + 1].y - TOP)+")", points[i + 1].x - LEFT+5, points[i + 1].y - TOP-15);
				else
					CUR_CANVAS.ctx.fillText("起点", points[i].x - LEFT, points[i].y - TOP);
				CUR_CANVAS.ctx.fill();
				CUR_CANVAS.ctx.stroke();
				CUR_CANVAS.ctx.closePath();
			}
			if (points[i + 2] == null) {
				traingle(points[i].x - LEFT, points[i].y - TOP, points[i + 1].x - LEFT, points[i + 1].y - TOP);
				CUR_CANVAS.ctx.moveTo(points[i + 1].x - LEFT, points[i + 1].y - TOP);
				CUR_CANVAS.ctx.lineTo(x4, y4);
				CUR_CANVAS.ctx.lineTo(x5, y5);
				if (CUR_CANVAS.withPosition)
					CUR_CANVAS.ctx.fillText("("+(points[i + 1].x - LEFT) + "," + (points[i + 1].y - TOP)+")", points[i + 1].x - LEFT+5, points[i + 1].y - TOP-15);
				else
					CUR_CANVAS.ctx.fillText("終点", points[i + 1].x - LEFT, points[i + 1].y - TOP);
				CUR_CANVAS.ctx.fill();
				CUR_CANVAS.ctx.stroke();
				CUR_CANVAS.ctx.closePath();
			}
			CUR_CANVAS.ctx.beginPath();
			CUR_CANVAS.ctx.lineWitdh = 4;
			CUR_CANVAS.ctx.moveTo(points[i].x - LEFT, points[i].y - TOP);
			if (CUR_CANVAS.withPosition)
				CUR_CANVAS.ctx.fillText("("+(points[i].x - LEFT) + "," + (points[i].y - TOP)+")", points[i].x - LEFT +5, points[i].y - TOP-15);
			CUR_CANVAS.ctx.arc(points[i].x - LEFT, points[i].y - TOP, 3, 0, 2 * Math.PI, false);
			CUR_CANVAS.ctx.lineTo(points[i + 1].x - LEFT, points[i + 1].y - TOP);
			CUR_CANVAS.ctx.stroke(); // 画线 \
		}
	},
	drawCircle : function(points) {
		CUR_CANVAS.ctx.fillStyle = CUR_CANVAS.color;
		CUR_CANVAS.ctx.beginPath();
		if (!points.length) {
			CUR_CANVAS.ctx.arc(points.x - LEFT, points.y - TOP, 3, 0, 2 * Math.PI, false);
			return;
		}
		for ( var i = 0; i < points.length; i++)
			CUR_CANVAS.ctx.arc(points[i].x - LEFT, points[i].y - TOP, 3, 0, 2 * Math.PI, false);
		CUR_CANVAS.ctx.fill();
		CUR_CANVAS.ctx.closePath();
		return;
	},
	clearCanvas : function() {
		CUR_CANVAS.ctx.clearRect(0, 0, CUR_CANVAS.canvas.width, CUR_CANVAS.canvas.height);
	}
};